﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Utilities;
using Common.Library;
using OnlineVideos.View_Models;
using OnlineVideos.Entities;
using OnlineVideos.Data;
using OnlineVideos;
using Common.Utilities;

namespace OnlineVideos
{

    public partial class VideoGameDescription : PhoneApplicationPage
    {
        List<VideoGameProperties> Gamelist = null;
        public VideoGameDescription()
        {
            InitializeComponent();
            Gamelist = new List<VideoGameProperties>();
            Loaded += new RoutedEventHandler(WeaponsDescription_Loaded);
        }

        void WeaponsDescription_Loaded(object sender, RoutedEventArgs e)
        {
           
            string id = "";
            string wid = "";
            string type = "";
            if (NavigationContext.QueryString.TryGetValue("wid", out wid))
            {
                if (NavigationContext.QueryString.TryGetValue("type", out type))
                { }
                if (NavigationContext.QueryString.TryGetValue("id", out id))

                    LayoutRoot.Background = ShowDetail.LoadShowPivotBackground(Convert.ToInt32(id));
                Gamelist = Loaddescription(wid, id, type);
                if (Gamelist.Count > 0)
                {
                    tblkmsg.Visibility = Visibility.Collapsed;

                }
                else
                {
                    tblkmsg.Visibility = Visibility.Visible;
                }
            }
        }

        private List<VideoGameProperties> Loaddescription(string id, string showid, string type)
        {
            if (type == "Weapon")
            {
                LoadWeapons(id, showid);
            }
            if (type == "Vehicle")
            {
                loadVechiles(id, showid);
            }
            if (type == "Achievements")
            {
                loadAchievements(id, showid);
            }
            if (type == "Misions")
            {
                loadMissions(id, showid);
            } 
            if (type == "Cast")
            {
                loadCast(id, showid);
            }
            if (type == "Controls")
            {
                loadControls(id, showid);
            }

            return Gamelist;
        }

        private void loadControls(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameControls> descList = from k in context.ShowGameControls where k.ControlId == Convert.ToInt32(id) & k.ShowID == Convert.ToInt32(showid) select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.ControlName;
                objvideoprop.Name = item.ControlName;
                if (item.Controldescription != string.Empty)
                {
                    tblkDescription.Text = item.Controldescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadCast(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<CastProfile> descList = from k in context.PersonProfile where k.PersonID == Convert.ToInt32(id)  select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadMissions(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameMissions> descList = from k in context.ShowMissions where k.MissionID == Convert.ToInt32(id) & k.ShowID == Convert.ToInt32(showid) select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.MissionName;
                objvideoprop.Name = item.MissionName;
                if (item.Missiondescription != string.Empty)
                {
                    string data = item.Walkthrough;
                    if (data != null)
                    {
                        tblkname.Visibility = Visibility.Visible;
                        tblkvalue.Visibility = Visibility.Visible;
                        tblkname.Text = "Walkthrough : ";
                        tblkvalue.Text = item.Walkthrough;
                    }
                    tblkDescription.Text = item.Missiondescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }


        private void loadAchievements(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameAchievement> descList = from k in context.ShowAchievements where k.AchievementID == Convert.ToInt32(id) & k.ShowID == Convert.ToInt32(showid) select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.AchievementName;
                objvideoprop.Name = item.AchievementName;
                if (item.AchievementDescription != string.Empty)
                {
                    string data = item.Points;
                    if (data != null)
                    {
                        tblkname.Visibility = Visibility.Visible;
                        tblkvalue.Visibility = Visibility.Visible;
                        tblkname.Text = "Points : ";
                        tblkvalue.Text = item.Points;
                    }
                    tblkDescription.Text = item.AchievementDescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadVechiles(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameVehicles> descList = from k in context.ShowVehicle where k.VehicleID == Convert.ToInt32(id) & k.ShowID == Convert.ToInt32(showid) select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }
            }
        }

        private void LoadWeapons(string id, string showid)
        {
            OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            IQueryable<GameWeapons> descList = from k in context.ShowWeapons where k.WeaponID == Convert.ToInt32(id) & k.ShowID == Convert.ToInt32(showid) select k;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }
            }
        }
       
    }

}